//
//  BSSFileManager.m
//  BrightSunnySky
//
//  Created by zhoujunchun on 2019/3/8.
//  Copyright © 2019 PURANG. All rights reserved.
//

#import "BSSFileManager.h"
#import "YYModel.h"
#import "NSString+Judge.h"

static BSSFileManager* fileManager;

static NSString* userInformationFilepath;

@implementation BSSFileManager

+ (BSSFileManager *)shareBSSFileManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        fileManager = [[BSSFileManager alloc]init];
        NSArray* fileArray = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
        NSString* documentDirectory = [fileArray objectAtIndex:0];
        [fileManager createDirectoryAtPath:documentDirectory withIntermediateDirectories:YES attributes:nil error:nil];
        userInformationFilepath = [documentDirectory stringByAppendingPathComponent:@"userInformation"];
        
    });
    return fileManager;
}

- (BOOL)checkUserInformationFileExist
{
    return [fileManager fileExistsAtPath:userInformationFilepath];
}

- (void)creatFileUserInformationFile
{
    if (![self checkUserInformationFileExist])
    {
        [fileManager createFileAtPath:userInformationFilepath contents:nil attributes:nil];
    }
}

- (NSMutableDictionary*)readUserInformationFile
{
    return (NSMutableDictionary*)[NSKeyedUnarchiver unarchiveObjectWithFile:userInformationFilepath];
}

- (void)writeUserInformationTofileWithUserInformation:(NSMutableDictionary*)userInformation
{
    if (![self checkUserInformationFileExist])
    {
        [self creatFileUserInformationFile];
        [NSKeyedArchiver archiveRootObject:userInformation toFile:userInformationFilepath];
    }
    else
    {
        NSMutableDictionary* dictionary = [self readUserInformationFile];
        NSArray* array = [userInformation allKeys];
        for (NSString* key in array)
        {
            [dictionary setObject:[userInformation valueForKey:key] forKey:key];
        }
        [NSKeyedArchiver archiveRootObject:dictionary toFile:userInformationFilepath];
    }
}

- (void)removeUserInformationFile
{
    if ([self checkUserInformationFileExist])
    {
        [fileManager removeItemAtPath:userInformationFilepath error:nil];
    }
}

- (BSSPersonModel *)getLoginModel{
    BSSPersonModel *model = [BSSPersonModel yy_modelWithDictionary:[self readUserInformationFile]];
    return model;
}

- (BOOL)getUserIsContainUsertype:(NSString *)userType{
    
    BSSPersonModel *model = [BSSPersonModel yy_modelWithDictionary:[self readUserInformationFile]];
    if ([NSString bigString:model.userType containString:userType]) {
        return YES;
    }
    return NO;
}


- (BOOL)isPersonage{
    return [self getUserIsContainUsertype:@"0"];
}
- (BOOL)isMerchant
{
    return [[self getLoginModel].isMerchant isEqualToString:@"1"];;
}
- (BOOL)isCompanyAccount{
    return [self getUserIsContainUsertype:@"1"];
}
- (BOOL)isBackgroundt{
    return [self getUserIsContainUsertype:@"2"];
}

- (BOOL)isPersonSex
{
    return [[self getLoginModel].sex isEqualToString:@"1"];;
}
- (BOOL)needIdCertified
{
    return ![[self getLoginModel].idCertified isEqualToString:@"1"];
}
- (BOOL)isOpenCreditOn
{
    return [[self getLoginModel].creditOn isEqualToString:@"1"];
}

- (BOOL)isCreditCustomerManager{
    return [self getUserPermission:@"flow_credit_customer_manager"];
}
- (BOOL)isCreditManager{
    return [self getUserPermission:@"flow_credit_manager"];
}
- (BOOL)isCreditBranc{
    return [self getUserPermission:@"flow_credit_branch"];
}
- (BOOL)isCreditRisk{
    return [self getUserPermission:@"flow_credit_risk"];
}
- (BOOL)isCreditCenter{
    return [self getUserPermission:@"flow_credit_center"];
}
- (BOOL)isCreditCommittee{
    return [self getUserPermission:@"flow_credit_committee"];
}
- (BOOL)isCreditDirector{
    return [self getUserPermission:@"flow_credit_director"];
}
- (BOOL)isCustomerManager{
    return [self getUserPermission:@"flow_loan_customer_manager"];
}
- (BOOL)isLoanManager{
    return [self getUserPermission:@"flow_loan_manager"];
}
- (BOOL)isLoanBranch{
    return [self getUserPermission:@"flow_loan_branch"];
}
- (BOOL)isLoanRisk{
    return [self getUserPermission:@"flow_loan_risk"];
}
- (BOOL)isLoanCenter{
    return [self getUserPermission:@"flow_loan_center"];
}
- (BOOL)isLoanCommittee{
    return [self getUserPermission:@"flow_loan_committee"];
}
- (BOOL)isLoanDirector{
    return [self getUserPermission:@"flow_loan_director"];
}

- (BOOL)getUserPermission:(NSString *)permission{
    
    BSSPersonModel *model = [BSSPersonModel yy_modelWithDictionary:[self readUserInformationFile]];
    if ([NSString bigString:model.permission containString:permission]) {
        return YES;
    }
    return NO;
}

@end
